﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Yuebon.Car.IServices;
using Yuebon.Car.Models;
using Yuebon.CMS.Application;
using Yuebon.CMS.Dtos;
using Yuebon.CMS.IServices;
using Yuebon.CMS.Models;
using Yuebon.Commons;
using Yuebon.Commons.Helpers;
using Yuebon.Commons.Models;
using Yuebon.WebApi.Areas.CMS.Models;
using Yuebon.WebApi.Common;
using Yuebon.WebApi.Controllers;
using Yuebon.WebApi.Models;

namespace Yuebon.WebApi.Areas.Car.Controllers
{
    /// <summary>
    /// 汽车品牌
    /// </summary>
    [Route("api/Car/[controller]")]
    [ApiController]

    public class AutoBrandController : BusinessApiController<AutoBrand, IAutoBrandService>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="_iService"></param>
        public AutoBrandController(IAutoBrandService _iService) : base(_iService)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="info"></param>
        protected override void OnBeforeInsert(AutoBrand info)
        {
            info.Id = GuidUtils.CreateNo();
            info.CreatorTime = DateTime.Now;
            info.CreatorUserId = CurrentUser.Id;
            info.DeleteMark = false;
            info.CompanyId = CurrentUser.OrganizeId;
            info.DeptId = CurrentUser.DepartmentId;
            if (info.SortCode == null)
            {
                info.SortCode = 99;
            }
        }

        /// <summary>
        /// 在更新数据前对数据的修改操作
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        protected override void OnBeforeUpdate(AutoBrand info)
        {
            info.LastModifyTime = DateTime.Now;
            info.LastModifyUserId = CurrentUser.Id;
            if (info.SortCode == null)
            {
                info.SortCode = 99;
            }
        }

        /// <summary>
        /// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
        /// </summary>
        /// <param name="info">请求参数</param>
        /// <param name="token">token令牌</param>
        /// <returns>指定对象的集合</returns>
        [HttpPost("FindWithPagerFront")]
        public  IActionResult FindWithPagerFront(SearchModel info,string token)
        {
            //检查用户是否有权限，否则抛出MyDenyAccessException异常
            // CheckAuthorized(AuthorizeKey.ListKey);
            CommonResult result = new CommonResult();
            result = CheckToken(token);
            if (result.ErrCode == ErrCode.successCode)
            {
                string keywords = info.Keywords;
                string orderByDir = info.Order;
                string orderFlied =info.Sort;
                string where = GetPagerCondition();
                bool order = orderByDir == "asc" ? false : true;
                if (!string.IsNullOrEmpty(keywords))
                {
                    where += string.Format(" and (Title like '%{0}%' or SeoTitle like '%{0}%' or Tags like '%{0}%' or Zhaiyao like '%{0}%')", keywords);
                }

                PagerInfo pagerInfo = new PagerInfo();
                pagerInfo.PageSize = info.PageSize;
                pagerInfo.CurrenetPageIndex = info.PageNo;               
                List<AutoBrand> list = iService.FindWithPager(where, pagerInfo, orderFlied, order);
                result.Success = true;
                result.ResData = new
                {
                    recordsTotal = pagerInfo.RecordCount,
                    recordsFiltered = pagerInfo.RecordCount,
                    data = list
                };
            }
            return ToJsonContent(result);
        }

        /// <summary>
        /// 查询所有置顶的品牌
        /// </summary>
        /// <param name="token">token令牌</param>
        /// <returns>所有置顶的品牌</returns>
        [HttpGet("FindIsTopBrandList")]
        public IActionResult FindIsTopBrandList(string token)
        {
            //检查用户是否有权限，否则抛出MyDenyAccessException异常
            CommonResult result = new CommonResult();
            result = CheckToken(token);
            if (result.ErrCode == ErrCode.successCode)
            {
                string where = "IsTop=1";
                List<AutoBrand> list = iService.GetAllByIsNotDeleteAndEnabledMark(where).OrderBy(t => t.SortCode).ToList();
                List<AutoBrand> resultList = new List<AutoBrand>();
                foreach (AutoBrand info in list)
                {
                    AutoBrand autoBrand = new AutoBrand();
                    autoBrand = info;
                    autoBrand.ImgUrl = Configs.GetConfigurationValue("AppSetting", "FileUrl") + info.ImgUrl;
                    resultList.Add(autoBrand);
                }
                result.Success = true;
                result.ResData = resultList;
            }
            return ToJsonContent(result);
        }
    }
}
